var fs = require('fs');
var type = process.argv[2];
var readFileName = '/tmp/match_blog_url';
var writeFileName = '/tmp/url_migration_result.txt';
var rewriteConfFileName = '/etc/nginx/sudodev_ghost.rewrite';
var data = fs.readFileSync(readFileName,{encoding:'utf8'});
var paires = data.split('\n');
paires.forEach(function(line){
    console.log(line);
    var row_data = line.split('\t');
    if(row_data.length==3){
        var slug = row_data[0];
        var oldId = row_data[2];
        if(type=='rewrite'){
            var rule = 'rewrite /articles/' + oldId + '.html ' + "/" + slug + ' permanent;';
            fs.appendFileSync(rewriteConfFileName,rule+'\n');
        }else{
            var newUrl = 'http://www.sudodev.cn/'+slug;
            var oldUrl = 'http://www.sudodev.cn/articles/'+ oldId +'.html';
            var outputLine = oldUrl + ' ' + newUrl;
            console.log('==='+outputLine);
            fs.appendFileSync(writeFileName,outputLine+'\n');
        }
    }
});
